package logic

import (
	"context"
	"errors"
	"shuqi-novel/models/mysql"

	"shuqi-novel/shuqirpc/internal/svc"
	"shuqi-novel/shuqirpc/shuqirpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *shuqirpc.LoginRequest) (*shuqirpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	if err := user.FindUserByUsername(mysql.DB, in.Username); err != nil {
		return nil, errors.New("用户名不存在")
	}
	if in.Password != user.Password {
		return nil, errors.New("密码错误")
	}

	return &shuqirpc.LoginResponse{
		UserId: int64(user.ID),
	}, nil
}
